package com.szholly.pro.watergas_server.ps.android.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.util.CollectionUtils;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.ps.android.common.CommonSqlUtils;
import com.szholly.pro.watergas_server.ps.android.common.NonNullUtil;
import com.szholly.pro.watergas_server.ps.android.common.ResultUtils;
import com.szholly.pro.watergas_server.ps.android.common.SecurityConfig;
import com.szholly.pro.watergas_server.ps.android.common.ValidUtils;
import com.szholly.pro.watergas_server.ps.android.service.QcBatchService;
import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;

/**
 * 质控管理器
 * 
 * 2017/3/30 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */
public class QcService {
	private final static QcService INSTANCE = new QcService();
	private final QcBatchService mBatchService = QcBatchService.INSTANCE;

	private QcService() {
	}

	public static QcService get() {
		return INSTANCE;
	}

	/**
	 * 获取公司编号前缀
	 */
	public static String getSamplePrefix(String companyCode) throws Exception {
		return INSTANCE.getQC_COMPANYINFO(companyCode).getSafeString("SAMPLENUMBERRULE");
	}

	/**
	 * 获取任务列表
	 */
	public RowBase getTaskTypeData(String userId) throws Exception {
		RowBase result = new RowBase();
		String companyCode = null;
		SecurityConfig securitysConfig = SecurityConfig.get(userId);
		if (!securitysConfig.containQcLookAllCompany) {
			companyCode = CommonSqlUtils.getCompanyCode(userId);
			ValidUtils.hasText(companyCode, "没有公司权限");

			List<RowBase> companyList = getCompanyList();
			for (RowBase companyRow : companyList) {
				Object id = companyRow.get("id");
				if (companyCode.equals(id)) {
					result.put("companyBean", companyRow);
					break;
				}
			}
		}
		// 用于统计采样和实验
		Map<String, String> labAndSampleMap = mBatchService.getTaskCountGroupByTaskType(companyCode);
		Map<String, String> taskPendingMap = getJobCountGroupByJobCode(companyCode);
		taskPendingMap.putAll(labAndSampleMap);

		List<RowBase> taskTypeList = new ArrayList<RowBase>();
		if (securitysConfig.containTaskQcSample) {
			taskTypeList.add(QcControl.SAMPLE.clone());
		}
		if (securitysConfig.containTaskQcLab) {
			taskTypeList.add(QcControl.LAB.clone());
		}
		if (securitysConfig.containTaskQcDataContrast) {
			taskTypeList.add(QcControl.DATA_CONTRAST.clone());
		}
		if (securitysConfig.containTaskQcLocaleCheck) {
			taskTypeList.add(QcControl.LOCACLE_CHECK.clone());
		}
		if (securitysConfig.containTaskQcBlindCheck) {
			taskTypeList.add(QcControl.BLIND.clone());
		}
		if (securitysConfig.containTaskQcSpotContrast) {
			taskTypeList.add(QcControl.SPOT.clone());

		}
		if (securitysConfig.containTaskQcBuildBlindCheck) {
			taskTypeList.add(QcControl.BUILD_BLIND.clone());
		}
		if (securitysConfig.containTaskQcBuildSpotContrast) {
			taskTypeList.add(QcControl.BUILD_SPOT.clone());
		}
		for (RowBase taskTypeBean : taskTypeList) {
			String taskTypeCode = (String) taskTypeBean.get("id");
			taskTypeBean.put("taskCount", NonNullUtil.toString(taskPendingMap.get(taskTypeCode), "0"));
		}
		result.put("taskTypeList", taskTypeList);
		return result;
	}

	/**
	 * 获取现场考核任务列表
	 */
	public List<RowBase> getLocacleCheckTaskTypeData() throws Exception {
		List<RowBase> rows = new ArrayList<>();
		rows.add(QcControl.LOCACLE_CHECK_LAB);
		rows.add(QcControl.LOCACLE_CHECK_SAMPLE);
		rows.add(QcControl.LOCACLE_CHECK_OPERATION);
		Map<String, String> taskPendingMap = getJobCountGroupByJobCode(null);
		for (RowBase taskTypeBean : rows) {
			String taskTypeCode = (String) taskTypeBean.get("id");
			taskTypeBean.put("taskCount", NonNullUtil.toString(taskPendingMap.get(taskTypeCode), "0"));
		}
		return rows;
	}

	/**
	 * 获取公司列表
	 */
	public List<RowBase> getCompanyData(String userCode, String taskTypeCode) throws Exception {
		List<RowBase> companyList;

		if (QcControl.isLocacleChildCheck(taskTypeCode)) {
			companyList = getLocaleCheckCompanyList(userCode, taskTypeCode);
		} else if (QcControl.DATA_CONTRAST.equals(taskTypeCode)) {
			companyList = getDataConstrastCompanyList();
		} else {
			companyList = getCompanyList();
		}

		// 实验界面 盲样抽测界面 现场考核实验室检查界面 隐藏点位的显示
		boolean hidePointCount = QcControl.LAB.equals(taskTypeCode)
				|| QcControl.BLIND.equals(taskTypeCode)
				|| QcControl.SPOT.equals(taskTypeCode)
				|| QcControl.LOCACLE_CHECK_LAB.equals(taskTypeCode);
		if (hidePointCount) {
			for (RowBase rowBase : companyList) {
				rowBase.remove("count");
			}
		}
		Map<String, String> jobCountMap;
		if (QcControl.LAB.equals(taskTypeCode)|| QcControl.SAMPLE.equals(taskTypeCode)) {
			jobCountMap =mBatchService.getTaskCountGroupByCompany(taskTypeCode);
		}else{
			jobCountMap = getJobCountGroupByCompanyCode(taskTypeCode);
		}

		for (RowBase companyBean : companyList) {
			String id = (String) companyBean.get("id");
			String count = jobCountMap.get(id);
			companyBean.put("taskCount", NonNullUtil.toString(count, "0"));
		}
		return companyList;
	}

	/**
	 * 获取点位列表
	 */
	public List<RowBase> getPointData(String taskTypeCode, String companyCode) throws Exception {
		ValidUtils.hasText(companyCode);
		ValidUtils.hasText(taskTypeCode);
		List<RowBase> list = getPointList(companyCode);
		if (QcControl.SAMPLE.equals(taskTypeCode)) {// 采样界面的点位信息
			return list;
		}
		Map<String, String> jobCountMap = getJobCountGroupByPointCode(taskTypeCode, companyCode);
		for (RowBase pointBean : list) {
			String id = (String) pointBean.get("id");
			String count = jobCountMap.get(id);
			pointBean.put("taskCount", NonNullUtil.toString(count, "0"));
		}
		return list;
	}

	/**
	 * 获取监测
	 */
	public List<RowBase> getMonitorTypeData(String companyCode) throws Exception {
		List<GlobalParameter> parms = new ArrayList<>();
		parms.add(new GlobalParameter("companyCode", companyCode));
		String sql = "select distinct t1.pointtype from qc_pointinfo t1 where t1.companycode = @companyCode";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		Set<String> monitorTypeCodeSet = new HashSet<>();
		if (!CollectionUtils.isEmpty(rows)) {
			for (RowBase bean : rows) {
				String monitorTypeCode = bean.getSafeString("pointtype");
				monitorTypeCodeSet.add(monitorTypeCode);
			}
		}

		List<RowBase> resultList = new ArrayList<>();
		List<RowBase> monitorTypeList = getMonitorTypeList();
		for (RowBase monitorType : monitorTypeList) {
			if (monitorTypeCodeSet.contains(monitorType.get("id"))) {
				resultList.add(monitorType);
			}
		}
		return resultList;
	}

	/**
	 * 获取采样质控类型列表
	 */
	public List<RowBase> getSampleQcTypeList() throws Exception {
		List<RowBase> resultList = new ArrayList<>();
		List<RowBase> qcTypeList = getQcTypeList();
		for (RowBase qcType : qcTypeList) {
			if ("采样".equals(qcType.get("key"))) {
				resultList.add(qcType);
			}
		}
		return resultList;
	}

	/**
	 * 获取实验质控类型列表
	 */
	public List<RowBase> getLabQcTypeList() throws Exception {
		List<RowBase> qcTypeList = getQcTypeList();
		return qcTypeList;
	}

	/**
	 * 校验样品编号是否可用
	 * 
	 * @param sampleId
	 */
	public String testPXYSampleId(String sampleId, String companyCode) {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("sampleId", sampleId));
		parms.add(new GlobalParameter("companyCode", companyCode));
		String sql = "select case\n"
				+ "         when t1.ispxy = '0' then\n"
				+ "          t1.samplenumber\n"
				+ "         when t1.ispxy = '1' then\n"
				+ "          t1.PXY_SAMPLENUMBER\n"
				+ "       end ID,\n"
				+ "       case\n"
				+ "         when t1.ispxy = '0' then\n"
				+ "          t2.samplenumber\n"
				+ "         when t1.ispxy = '1' then\n"
				+ "          t1.samplenumber\n"
				+ "       end PXY_ID\n"
				+ "  from QC_SAMPLEDATA t1\n"
				+ "  left join QC_SAMPLEDATA t2\n"
				+ "    on t2.PXY_SAMPLENUMBER = @sampleId\n"
				+ "   and t2.company = @companyCode\n"
				+ " where 1 = 1\n"
				+ "   and t1.samplenumber = @sampleId\n"
				+ "   and t1.company = @companyCode";
		try {
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows != null && rows.size() != 0) {
				RowBase rowBase = rows.get(0);
				String id = rowBase.getSafeString("ID");
				String pxyId = rowBase.getSafeString("PXY_ID");
				if (!StringUtils.IsNullOrSpace(id) && !StringUtils.IsNullOrSpace(pxyId)) {
					Map<String, Object> resultMap = new HashMap<>();
					resultMap.put("sampleId", id);
					resultMap.put("sampleIdExtra", pxyId);
					return ResultUtils.getToastResult(resultMap);
				} else {
					return ResultUtils.getToastResult("编号不存在");
				}
			} else {
				return ResultUtils.getToastResult("编号不存在");
			}
		} catch (Exception e) {
			return ResultUtils.getToastResult("匹配异常", e);
		}
	}

	/**
	 * 校验样品编号是否可用
	 * 
	 * @param sampleId
	 */
	public String testNormalId(String sampleId, String companyCode) {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("sampleId", sampleId));
		parms.add(new GlobalParameter("companyCode", companyCode));
		String sql = "select t.samplenumber ID, null PXY_ID\n"
				+ "  from QC_SAMPLEDATA t\n"
				+ " where 1 = 1\n"
				+ "   and samplenumber = @sampleId\n"
				+ "   and t.company = @companyCode";
		try {
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows != null && rows.size() != 0) {
				RowBase rowBase = rows.get(0);
				String id = rowBase.getSafeString("ID");
				if (!StringUtils.IsNullOrSpace(id)) {
					Map<String, Object> resultMap = new HashMap<>();
					resultMap.put("sampleId", id);
					return ResultUtils.getToastResult(resultMap);
				} else {
					return ResultUtils.getToastResult("编号不存在");
				}
			} else {
				return ResultUtils.getToastResult("编号不存在");
			}
		} catch (Exception e) {
			return ResultUtils.getToastResult("匹配异常", e);
		}
	}

	/**
	 * 获取实验方法
	 */
	public List<RowBase> getLabMethodList(String companyCode) throws Exception {
		ValidUtils.hasText(companyCode, "公司编号为空");
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("companyCode", companyCode));
		String sql = "select t.METHODCODE       \"methodCode\",\n"
				+ "       t.METHODNAME       \"methodName\",\n"
				+ "       t.MONITORPARAMCODE \"parameterCode\",\n"
				+ "       t.monitortypecode  \"monitorTypeCode\"\n"
				+ "  from qc_expmethod t\n"
				+ " where 1 = 1\n"
				+ "   and t.companycode = @companyCode";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		return NonNullUtil.toList(rows);
	}

	/**
	 * 获取人员信息
	 * 
	 * @param personType
	 *            0.实验员 1.采样人 3.专家组现场考核人员
	 */
	public List<RowBase> getPersonList(String companyId, String personType) {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("personType", personType));
		String sql = "select t.PERSONCODE \"id\",\n"
				+ "       t.PERSONNAME \"name\",\n"
				+ "       t.COMPANYCODE \"companyId\",\n"
				+ "       case t.PERSONTYPE\n"
				+ "         when '0' then\n"
				+ "          '实验'\n"
				+ "         when '1' then\n"
				+ "          '采样'\n"
				+ "         when '3' then\n"
				+ "          '专家组'\n"
				+ "       end \"type\"\n"
				+ "  from QC_COMPANYPERSONINFO t\n"
				+ " where 1 = 1\n"
				+ "   and t.PERSONTYPE = @personType\n";
		if (!StringUtils.IsNullOrSpace(companyId)) {
			parms.add(new GlobalParameter("companyId", companyId));
			sql += "   and t.companycode = @companyId\n";
		}
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		return NonNullUtil.toList(rows);
	}

	/**
	 * 获取人员信息
	 * 
	 * @param personType
	 *            0.实验员 1.采样人 3.专家组现场考核人员
	 */
	public List<RowBase> getPersonList(String personType) {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("personType", personType));
		String sql = "select max(t.PERSONCODE) \"id\",\n"
				+ "       t.PERSONNAME \"name\",\n"
				+ "       case max(t.PERSONTYPE)\n"
				+ "         when '0' then\n"
				+ "          '实验'\n"
				+ "         when '1' then\n"
				+ "          '采样'\n"
				+ "         when '3' then\n"
				+ "          '专家组'\n"
				+ "       end \"type\"\n"
				+ "  from QC_COMPANYPERSONINFO t\n"
				+ " where 1 = 1\n"
				+ "   and t.PERSONTYPE = @personType\n"
				+ " group by t.PERSONNAME";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		return NonNullUtil.toList(rows);
	}

	// 获取公司列表
	private List<RowBase> getCompanyList() throws Exception {
		String sql = "select t.COMPANYCODE \"id\",\n"
				+ "       t.COMPANYNAME \"name\",\n"
				+ "       t.COMPANYTYPE \"type\",\n"
				+ "       t.SAMPLENUMBERRULE \"extra\",\n"
				+ "       NVL(t1.COUNT, '0') AS \"count\"\n"
				+ "  from QC_COMPANYINFO t\n"
				+ "  left join (select count(COMPANYCODE) COUNT, COMPANYCODE\n"
				+ "               from QC_POINTINFO\n"
				+ "              group by COMPANYCODE) t1\n"
				+ "    on t1.COMPANYCODE = t.COMPANYCODE\n"
				+ " where 1 = 1\n"
				+ "   and t.COMPANYTYPE in ('0','1')\n";
		List<RowBase> rows = DataUtil.getSelectRows(sql, null);
		return NonNullUtil.toList(rows);
	}

	// 获取数据比对公司列表
	private List<RowBase> getDataConstrastCompanyList() throws Exception {
		List<RowBase> resultList = new ArrayList<>();
		List<RowBase> companyList = getCompanyList();
		for (RowBase rowBase : companyList) {
			String companyCode = (String) rowBase.get("id");
			if ("1".equals(companyCode)) {
				resultList.add(rowBase);
			}
		}
		return resultList;
	}

	// 获取现场考核公司列表
	private List<RowBase> getLocaleCheckCompanyList(String userId, String taskTypeCode) throws Exception {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("userId", userId));
		String sql = "select distinct t.station_id\n"
				+ "  from operation_group_station_asso t\n"
				+ "  left join safe_user t1\n"
				+ "    on t1.s_orgid = t.operation_group_id\n"
				+ " where t1.s_userid = @userId";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		Set<String> companyCodeSet = new HashSet<>();
		if (!CollectionUtils.isEmpty(rows)) {
			for (RowBase bean : rows) {
				String companyCode = bean.getSafeString("station_id");
				companyCodeSet.add(companyCode);
			}
		}

		List<RowBase> locacleCheckCompanyList = new ArrayList<>();
		List<RowBase> companyList = getCompanyList();
		for (RowBase rowBase : companyList) {
			if (companyCodeSet.contains(rowBase.get("id"))) {
				locacleCheckCompanyList.add(rowBase);
			}
		}

		// 再增加一层筛选--不同任务类型对应不同的公司列表
		List<RowBase> resultList = new ArrayList<>();
		for (RowBase rowBase : locacleCheckCompanyList) {
			String companyCode = (String) rowBase.get("id");
			if (QcControl.LOCACLE_CHECK_LAB.equals(taskTypeCode)) {
				if (!"1".equals(companyCode)) {
					resultList.add(rowBase);
				}
			} else if (QcControl.LOCACLE_CHECK_OPERATION.equals(taskTypeCode)) {
				if ("1".equals(companyCode)) {
					resultList.add(rowBase);
				}
			} else if (QcControl.LOCACLE_CHECK_SAMPLE.equals(taskTypeCode)) {
				if (!"1".equals(companyCode)) {
					resultList.add(rowBase);
				}
			}
		}
		return resultList;
	}

	// 获取点位列表
	private List<RowBase> getPointList(String companyCode) {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("companyCode", companyCode));
		String sql = "select t.POINTCODE \"id\",\n"
				+ "       t1.SAMPLE_POSITION \"name\",\n"
				+ "       t.companycode \"fatherId\",\n"
				+ "       t.pointtype \"type\"\n"
				+ "  from QC_POINTINFO t\n"
				+ "  left join BC_SITE_INFO t1\n"
				+ "    on t.pointcode = t1.code\n"
				+ " where t.companycode = @companyCode";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		return NonNullUtil.toList(rows);
	}

	// 待处理任务数 Map<任务编号,任务数>
	private Map<String, String> getJobCountGroupByJobCode(String companyCode) throws Exception {
		Map<String, String> result = new HashMap<>();
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		// parms.add(new GlobalParameter("sample",
		// QcControl.SAMPLE.getJobId()));
		// parms.add(new GlobalParameter("lab", QcControl.LAB.getJobId()));
		parms.add(new GlobalParameter("data", QcControl.DATA_CONTRAST.getJobId()));
		parms.add(new GlobalParameter("blind", QcControl.BLIND.getJobId()));
		parms.add(new GlobalParameter("spot", QcControl.SPOT.getJobId()));
		parms.add(new GlobalParameter("check1", QcControl.LOCACLE_CHECK_LAB.getJobId()));
		parms.add(new GlobalParameter("check2", QcControl.LOCACLE_CHECK_SAMPLE.getJobId()));
		parms.add(new GlobalParameter("check3", QcControl.LOCACLE_CHECK_OPERATION.getJobId()));
		String normalSql = "select count(t.job_id) count, t.job_id\n"
				+ "  from OP_TASK t\n"
				+ " where 1 = 1\n"
				+ "   and t.job_id in (@data,@check1,@check2,@check3,@blind,@spot)\n"
				+ "   and t.task_status = '0'\n";

		String blindSql = "select count(t.job_id) count, t.job_id\n"
				+ "  from OP_TASK t\n"
				+ "  left join qc_water_than t1\n"
				+ "    on t1.operation_task_code = t.task_code\n"
				+ " where t1.value is null\n"
				+ "   and t1.companyid is not null\n"
				+ "   and t.task_status = '1'\n"
				+ "   and t.job_id in (@blind,@spot)\n";
		if (!StringUtils.IsNullOrSpace(companyCode)) {
			parms.add(new GlobalParameter("companyCode", companyCode));
			normalSql += "   and t.wwtp_id = @companyCode\n";
			blindSql += "   and t1.companyid = @companyCode\n";

		}
		normalSql += " group by t.job_id\n";
		blindSql += " group by t.job_id\n";

		List<RowBase> normalRows = DataUtil.getSelectRows(normalSql, parms);
		for (RowBase rowBase : normalRows) {
			String count = rowBase.getSafeString("count");
			String jobId = rowBase.getSafeString("job_id");
			String taskTypeCode = QcControl.getTaskTypeCode(jobId);
			if (QcControl.BLIND.equals(taskTypeCode)) {
				result.put(QcControl.BUILD_BLIND.getId(), count);
			} else if (QcControl.SPOT.equals(taskTypeCode)) {
				result.put(QcControl.BUILD_SPOT.getId(), count);
			} else {
				result.put(taskTypeCode, count);
			}
		}

		List<RowBase> blindRows = DataUtil.getSelectRows(blindSql, parms);
		for (RowBase rowBase : blindRows) {
			String count = rowBase.getSafeString("count");
			String jobId = rowBase.getSafeString("job_id");
			String taskTypeCode = QcControl.getTaskTypeCode(jobId);
			result.put(taskTypeCode, count);
		}
		return result;
	}

	// 待处理任务数 Map<公司编号,任务数>
	private Map<String, String> getJobCountGroupByCompanyCode(String taskTypeCode) throws Exception {
		Map<String, String> result = new HashMap<>();
		String jobId = QcControl.getTaskGuid(taskTypeCode);
		List<GlobalParameter> parms = new ArrayList<>();
		parms.add(new GlobalParameter("jobId", jobId));
		List<RowBase> rows = null;
		String sql = null;
		if (QcControl.DATA_CONTRAST.equals(taskTypeCode)
				|| QcControl.LOCACLE_CHECK_LAB.equals(taskTypeCode)
				|| QcControl.LOCACLE_CHECK_SAMPLE.equals(taskTypeCode)
				|| QcControl.LOCACLE_CHECK_OPERATION.equals(taskTypeCode)) {
			sql = "select count(t.wwtp_id) count, t.wwtp_id COMPANY_CODE\n"
					+ "  from OP_TASK t\n"
					+ " where 1 = 1\n"
					+ "   and t.task_status = '0'\n"
					+ "   and t.job_id = @jobId\n"
					+ "   and t.wwtp_id is not null\n"
					+ " group by t.wwtp_id";
		} else if (QcControl.BLIND.equals(taskTypeCode) || QcControl.SPOT.equals(taskTypeCode)) {
			sql = "select count(t1.companyid) count, t1.companyid COMPANY_CODE\n"
					+ "  from OP_TASK t\n"
					+ "  left join qc_water_than t1\n"
					+ "    on t1.operation_task_code = t.task_code\n"
					+ " where 1 = 1\n"
					+ "   and t.job_id = @jobId\n"
					+ "   and t.task_status = '1'\n"
					+ "   and t1.value is null\n"
					+ "   and t1.companyid is not null\n"
					+ " group by t1.companyid";
		}
		rows = DataUtil.getSelectRows(sql, parms);
		for (RowBase rowBase : rows) {
			String count = rowBase.getSafeString("count");
			String companyCode = rowBase.getSafeString("COMPANY_CODE");
			result.put(companyCode, count);
		}
		return result;
	}

	// 待处理任务数 Map<点位编号,任务数>
	private Map<String, String> getJobCountGroupByPointCode(String taskTypeCode, String companyCode) throws Exception {
		ValidUtils.hasText(taskTypeCode, "任务类型为空，任务数获取失败");
		ValidUtils.hasText(companyCode, "公司编号为空，任务数获取失败");
		Map<String, String> result = new HashMap<>();
		String jobId = QcControl.getTaskGuid(taskTypeCode);
		List<GlobalParameter> parms = new ArrayList<>();
		parms.add(new GlobalParameter("jobId", jobId));
		parms.add(new GlobalParameter("companyCode", companyCode));
		String sql = "select count(t.station_id) count, t.station_id\n"
				+ "  from OP_TASK t\n"
				+ " where 1 = 1\n"
				+ "   and t.task_status = '0'\n"
				+ "   and t.job_id = @jobId\n"
				+ "   and t.wwtp_id = @companyCode\n"
				+ "   and t.station_id is not null\n"
				+ " group by t.station_id";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		for (RowBase rowBase : rows) {
			String count = rowBase.getSafeString("count");
			String stationCode = rowBase.getSafeString("station_id");
			result.put(stationCode, count);
		}
		return result;
	}

	// 获取监测类型字典
	private List<RowBase> getMonitorTypeList() throws Exception {
		String sql = "select t.s_domaincode    as \"id\",\n"
				+ "       t.s_domaincaption as \"name\",\n"
				+ "       t.s_unit          as \"key\"\n"
				+ "  from DATA_DEFULATDOMAIN t\n"
				+ " where t.s_domainname = '质控监测类型'\n"
				+ "   and t.s_domaincode in (select distinct t.pointtype from qc_pointinfo t)\n"
				+ " order by to_number(t.s_domaincode) asc";
		List<RowBase> rows = DataUtil.getSelectRows(sql, null);
		return NonNullUtil.toList(rows);
	}

	// 获取质控类型字典项
	private List<RowBase> getQcTypeList() throws Exception {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String sql = "select t.s_domaincode    as \"id\",\n"
				+ "       t.s_domaincaption as \"name\",\n"
				+ "       t.s_unit          as \"key\"\n"
				+ "  from DATA_DEFULATDOMAIN t\n"
				+ " where t.s_domainname = '质控类型'";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		if (!CollectionUtils.isEmpty(rows)) {
			for (RowBase rowBase : rows) {
				String qcTypeName = (String) rowBase.get("name");
				if (qcTypeName.contains("（")) {
					qcTypeName = qcTypeName.replaceAll("（", "\n（");
				} else if (qcTypeName.contains("(")) {
					qcTypeName = qcTypeName.replaceAll("(", "\n(");
				}
				rowBase.put("name", qcTypeName);
			}
		}
		return NonNullUtil.toList(rows);
	}

	// 获取行 QC_COMPANYINFO
	private RowBase getQC_COMPANYINFO(String companyCode) throws Exception {
		ValidUtils.hasText(companyCode, "公司编号为空");
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("companyCode", companyCode));
		String sql = "select * from QC_COMPANYINFO t where 1 = 1 and t.companycode = @companyCode\n";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		ValidUtils.notEmpty(rows, "公司编号有误,无法获取公司信息");
		return rows.get(0);
	}
}
